<?php
/**
 * @ClassName: Leveluporder
 * @Description: 代理升级配置
 * @author: gydtrade
 * @Date: 2024-09-10 21:47
 * @Version: V1.0.0
 */

namespace app\admin\controller\agent;

use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;

class Leveluporder extends Controller
{
    use HasBuilder;

    protected $dataModel;

    protected function initialize()
    {
        parent::initialize();

        $this->dataModel = new model\AgentUpgradeConfig();
        $this->pageTitle = '推客升级配置';
        $this->enableField = 'status';
        $this->pagesize = 10;
    }

    /**
     * 构建搜索
     *
     * @return void
     */
    protected function buildSearch()
    {
        $search = $this->search;

        $search->text('name', '名称');
    }

    /**
     * 构建搜索条件
     * @return array
     */
    protected function filterWhere()
    {
        $searchData = request()->get();

        $where = [];
        if (isset($searchData['name']) && $searchData['name'] != '') {
            $where[] = ['name', 'like', '%' . trim($searchData['name']) . '%'];
        }

        return $where;
    }

    /**
     * 构建表格
     *
     * @return void
     */
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');
        $table->show('name', '名称');
        $table->show('low_height', '低到高升级奖励');
        $table->show('height_low', '高到低升级奖励');
        $table->show('score', '升级扣减积分');
        $table->show('diff_price', '代理升级差价');
        $table->show('description', '描述');
        $table->show('create_time', '创建时间');
        $table->show('update_time', '更新时间');

        $table->sortable('id, create_time');

        $table->getToolbar()
//            ->btnAdd()
            ->btnRefresh();

        $table->getActionbar()
            ->btnEdit()
            ->btnView();
    }

    /**
     * 构建表单
     *
     * @param boolean $isEdit
     * @param array $data
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;
        $form->html('推客升级下级代理配置');
        $form->hidden('id');
        $form->text('name', '名称')->maxlength(55)->required();
        $form->text('low_height', '低到高升级奖励')->required()->help('低等级招募高级代理，可以帮助自己冲量，职级相减就是奖励。（举例J0招到一个 要从J1升到J9算法是：J9 - J1＝8*100=800元；');
        $form->text('height_low', '高到低升级奖励')->required()->help('高等级招募低级代理，可以帮助自己冲量，职级相减就是奖励。（J9招到一个 要从J1升级到的J8算法是：J8 - J1＝7*100=800元；');
        $form->text('score', '升级扣减积分')->required()->help('代理帮下级设置等级 升级下级，扣减的积分，注意，只能设置小于自己的等级（举例 从J1升级到J4:J4-J1）=3*100=300积分');
        $form->text('diff_price', '代理升级差价')->required()->help('代理升级补差价，比如：（提升一个等级+500元补等级费（举例：J4升级到J9,需要补5个等级差价，即5*500=2500元）');
        $form->textarea('description', '描述')->maxlength(255);

        if ($isEdit) {
            $form->show('create_time', '创建时间');
            $form->show('update_time', '更新时间');
        }
    }

    /**
     * 保存数据
     *
     * @param integer $id
     * @return void
     */
    private function save($id = 0)
    {
        $data = request()->post();

        $result = $this->validate($data, []);

        if (!$result) {
            $this->error($result);
        }

        if ($id) {
            // 编辑
            $data['update_time'] = date_now();
        } else {
            // 新增
            $data['create_time'] = date_now();
            $data['update_time'] = date_now();
        }

        return $this->doSave($data, $id);
    }

}